package com.link.base.base.project.service;

import com.link.base.base.product.dao.mybatis.mapper.*;
import com.link.base.base.product.model.*;
import com.link.base.base.product.service.PartsCostService;
import com.link.base.base.product.service.ProdQuoteService;
import com.link.base.base.project.dao.mybatis.mapper.ProjectMapper;
import com.link.base.base.project.dao.mybatis.mapper.QuoteRoundMapper;
import com.link.base.base.project.model.QuoteRound;
import com.link.base.core.basic.util.BigDecimalUtils;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.JsonUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 报价轮次表
 *
 * @author 王昊
 * @date 2020-10-13 16:40:05
 */
@Service
public class QuoteRoundServiceImpl extends BasicServiceImpl<QuoteRound> implements QuoteRoundService {

    private static final Logger logger = LogManager.getLogger(QuoteRoundServiceImpl.class);

    @Resource
    private QuoteRoundMapper    quoteRoundMapper;

    @Resource
    private QuotePartsCostMapper quotePartsCostMapper;

    @Resource
    private PartsCostLineMapper partsCostLineMapper;

    @Resource
    private QuotePartsCostLineMapper quotePartsCostLineMapper;

    @Resource
    private ComputeQuotePartsCostMapper computeQuotePartsCostMapper;

    @Resource
    private ComputeQuotePartsCostLineMapper computeQuotePartsCostLineMapper;

    @Resource
    private PartsCostService partsCostService;

    @Resource
    private ProdQuoteService prodQuoteService;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public BasicMapper<QuoteRound> getBasicMapper() {
        return quoteRoundMapper;
    }


    /**
     * 新建一轮报价点击保存，
     * 报价轮次+1
     * 在零件报价表插入零件信息
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforInsert(QuoteRound entity) throws Exception {
        super.beforInsert(entity);
        //根据报价类型,项目ID查询报价信息，count list +1 赋值给当前报价轮次
        QuoteRound quote = new QuoteRound();
        quote.setProjectHeadId(entity.getProjectHeadId());
        quote.setQuoteType(entity.getQuoteType());
        List<QuoteRound> quoteRounds = quoteRoundMapper.queryByQuoteType(quote);
        long quoteRoundListSize = quoteRounds.size();
        quoteRoundListSize++;
        entity.setQuoteRound(quoteRoundListSize);
        //当前报价轮次还未生成，头ID为空，生成报价轮次头ID
        entity.setId(keyGenerateService.keyGenerate());
        //点击保存，插入零件报价
        if (entity.getQuoteRound() == 1) {
            //1.若当前报价轮次为第一轮，插入零件成本表信息-->零件报价表
            PartsCost partsCostEntity = new PartsCost();
            partsCostEntity.setProjectId(entity.getProjectHeadId());
            //1.1 查询总成件成本信息 (不用考虑当前报价类型，因为切换报价类型不会继承上一报价类型的最新报价信息)
            /** 对象变更
            List<PartsCost> partsCosts = partsCostService.queryByAssemblyParts(partsCostEntity);
            for (PartsCost prodCost : partsCosts) {
                ProdQuote prodQuote = new ProdQuote();
                //标记该零件报价为本轮次
                prodQuote.setQuoteRoundId(entity.getId());
                //**必须拿到零件成本ID。才能join到想要的成本价格。没有成本ID，查不到信息**
                prodQuote.setPartCostId(prodCost.getId());
                prodQuote.setcRMPartId(prodCost.getcRMPartId());
                prodQuote.setProjectHeadId(entity.getProjectHeadId());
                prodQuote.setWorkProcedure(prodCost.getWorkProcedure());
                prodQuote.setTrafficFee(prodCost.getFreight());
                prodQuote.setApportionment(prodCost.getApportionment());
                //模摊费用
                prodQuote.setMoldCost(prodCost.getMoldCost());
                prodQuote.setFactoryCostMold(prodCost.getFactoryCostMold());
                prodQuote.setNetWeight(prodCost.getProdNetWet());
                prodQuote.setWrapperType(prodCost.getContainerType());
                prodQuote.setContainerSize(prodCost.getContainerSize());
                prodQuote.setInputValue(prodCost.getInputContainer());
                prodQuote.setInputTotalAmount(prodCost.getTotalLnvest());
                prodQuote.setProdTotalNum(prodCost.getTotalProdOutput());
                prodQuote.setContainerLoss(prodCost.getVesselLoss());
                //周转容器单价
                prodQuote.setContainerUnitPrice(prodCost.getTurnoverContain());
                //专用设备费
                prodQuote.setSpecEquipFee(prodCost.getSpecialEquipCost());
                //实验费用
                prodQuote.setLaboratoryFee(prodCost.getExperimentCost());

                //总成材料价格
                prodQuote.setMaPrice(prodCost.getMaterialPrice());
                //总成外购费用
                prodQuote.setOutTotalPrice(prodCost.getOutBuyTotalPrice());
                //总成直接制造成本
                prodQuote.setMakeCost(prodCost.getManufactCost());
                //总成模具费
                prodQuote.setToolFee(prodCost.getMoldCost());
                //总成检具费
                prodQuote.setSubmitFee(prodCost.getInspectionCost());
                //出厂包装单件价格
                prodQuote.setShipPackUnitPrice(prodCost.getShipPackagePri());
                //工厂制造费用分摊
                prodQuote.setFacShareCost(prodCost.getFactoryCostAll());

                //设置特殊逻辑 不走后续的自动插入零件行信息
                prodQuote.setAttr1("noAfterInsert");
                prodQuoteService.insert(prodQuote);
            }

            //1.2 查询零部件成本信息

            List<PartsCost> partsCosts2 = partsCostService.queryByParts(partsCostEntity);
            for (PartsCost partsCost2 : partsCosts2) {
                ProdQuote prodQuote2 = new ProdQuote();
                //标记该零件报价为本轮次
                prodQuote2.setQuoteRoundId(entity.getId());
                //**必须拿到零件成本ID。才能join到想要的成本价格。没有成本ID，查不到信息**
                prodQuote2.setPartCostId(partsCost2.getId());
                prodQuote2.setCraft(partsCost2.getTechnology());
                prodQuote2.setNetWeight(partsCost2.getProdNetWet());
                prodQuote2.setRoughWeight(partsCost2.getProdGrossWet());
                prodQuote2.setMaPrice(partsCost2.getMaterialPrice());
                prodQuote2.setOutUnitPrice(partsCost2.getOutBuyUnitPrice());
                prodQuote2.setOutTotalPrice(partsCost2.getOutBuyTotalPrice());
                prodQuote2.setProdEquip(partsCost2.getProdEquipment());
                prodQuote2.setEquipHourCost(partsCost2.getEquipCostHour());
                prodQuote2.setPeoHourCost(partsCost2.getLaborCostHour());

                prodQuote2.setBeatTime(partsCost2.getProdBeatTime());
                prodQuote2.setCavityNum(partsCost2.getCavityNumber());
                prodQuote2.setWorkNum(partsCost2.getWorkerNum());

                prodQuote2.setPeoFee(partsCost2.getLaborCost());
                prodQuote2.setEquipFee(partsCost2.getEquipmentCost());
                prodQuote2.setEquipActivation(partsCost2.getEquUtilizatRate());
                prodQuote2.setPassPercent(partsCost2.getPassRate());
                prodQuote2.setClassProdNum(partsCost2.getShiftProd());

                prodQuote2.setMakeCost(partsCost2.getManufactCost());
                //模具费
                prodQuote2.setToolFee(partsCost2.getMouldCost());
                prodQuote2.setSubmitFee(partsCost2.getInspectionCost());
                prodQuote2.setSpecEquipFee(partsCost2.getSpecialEquipCost());
                //材料单价（元）
                prodQuote2.setMaUnitPrice(partsCost2.getMaterialUnitPri());

                prodQuote2.setAttr1("noAfterInsert");

                prodQuoteService.insert(prodQuote2);

            }
             **/
        } else {
            //2.若当前报价轮次非第一轮，取零件报价表当前报价类型报价轮次最高的成本信息
            //2.1根据当前项目ID，报价类型 查询零件报价表信息
            ProdQuote prodQuote = new ProdQuote();
            prodQuote.setProjectHeadId(entity.getProjectHeadId());
            prodQuote.setQuoteType(entity.getQuoteType());
            //2.2增加查询条件：报价轮次为当前报价轮次-1  定位上一报价轮次
            long nowQuoteRound = entity.getQuoteRound();
            nowQuoteRound--;
            prodQuote.setQuoteRound(nowQuoteRound);
            List<ProdQuote> prodQuotes = prodQuoteService.queryByProType(prodQuote);
           //2.3插入信息到报价表(不分总成或零件，全部插入，前端展示再区分)
            for (ProdQuote prodQuote1 : prodQuotes) {
                ProdQuote prodQuote2 = new ProdQuote();
                //标记该零件报价为本轮次
                prodQuote2.setQuoteRoundId(entity.getId());
                //**必须拿到零件成本ID。才能join到想要的成本价格。没有成本ID，查不到信息**
                prodQuote2.setDefaultRate(prodQuote1.getDefaultRate());
                prodQuote2.setDefineRate(prodQuote1.getDefineRate());
                prodQuote2.setPartCostId(prodQuote1.getPartCostId());
                prodQuote2.setPartCode(prodQuote1.getPartCode());
                prodQuote2.setPartName(prodQuote1.getPartName());
                prodQuote2.setiYUPartName(prodQuote1.getiYUPartName());
                prodQuote2.setIsDBPart(prodQuote1.getIsDBPart());
                prodQuote2.setPartSize(prodQuote1.getPartSize());
                prodQuote2.setPerCar(prodQuote1.getPerCar());
                prodQuote2.setLoadingRatio(prodQuote1.getLoadingRatio());

                prodQuote2.setWorkProcedure(prodQuote1.getWorkProcedure());
                prodQuote2.setFactoryCost(prodQuote1.getFactoryCost());
//                prodQuote2.setApportionment(prodQuote1.getApportionment());
                //模摊费用
//                prodQuote2.setMoldCost(prodQuote1.getMoldCost());
//                prodQuote2.setFactoryCostMold(prodQuote1.getFactoryCostMold());
                prodQuote2.setNetWeight(prodQuote1.getNetWeight());
                prodQuote2.setWrapperType(prodQuote1.getWrapperType());
                prodQuote2.setContainerSize(prodQuote1.getContainerSize());
                prodQuote2.setInputValue(prodQuote1.getInputValue());
                prodQuote2.setInputTotalAmount(prodQuote1.getInputTotalAmount());
                prodQuote2.setProdTotalNum(prodQuote1.getProdTotalNum());
                prodQuote2.setContainerLoss(prodQuote1.getContainerLoss());
                prodQuote2.setContainerUnitPrice(prodQuote1.getContainerUnitPrice());


                prodQuote2.setMaterialBrand(prodQuote1.getMaterialBrand());
                prodQuote2.setCraft(prodQuote1.getCraft());
                prodQuote2.setRoughWeight(prodQuote1.getRoughWeight());
                prodQuote2.setMaPrice(prodQuote1.getMaPrice());
                prodQuote2.setOutUnitPrice(prodQuote1.getOutUnitPrice());
                prodQuote2.setProdEquip(prodQuote1.getProdEquip());
                prodQuote2.setEquipHourCost(prodQuote1.getEquipHourCost());
                prodQuote2.setPeoHourCost(prodQuote1.getPeoHourCost());

                prodQuote2.setBeatTime(prodQuote1.getBeatTime());
                prodQuote2.setCavityNum(prodQuote1.getCavityNum());
                prodQuote2.setWorkNum(prodQuote1.getWorkNum());

                prodQuote2.setPeoFee(prodQuote1.getPeoFee());
                prodQuote2.setEquipFee(prodQuote1.getEquipFee());
                prodQuote2.setEquipActivation(prodQuote1.getEquipActivation());
                prodQuote2.setPassPercent(prodQuote1.getPassPercent());
                prodQuote2.setClassProdNum(prodQuote1.getClassProdNum());

                prodQuote2.setMakeCost(prodQuote1.getMakeCost());
                //模具费
                prodQuote2.setToolFee(prodQuote1.getToolFee());
                prodQuote2.setSubmitFee(prodQuote1.getSubmitFee());
                prodQuote2.setSpecEquipFee(prodQuote1.getSpecEquipFee());


                prodQuote2.setMaUnitPrice(prodQuote1.getMaUnitPrice());
                prodQuote2.setOutTotalPrice(prodQuote1.getOutTotalPrice());
                prodQuote2.setShipPackUnitPrice(prodQuote1.getShipPackUnitPrice());
                prodQuote2.setLaboratoryFee(prodQuote1.getLaboratoryFee());
                prodQuote2.setTrafficFee(prodQuote1.getTrafficFee());
                prodQuote2.setManaProFee(prodQuote1.getManaProFee());
                prodQuote2.setAcctPrice(prodQuote1.getAcctPrice());
                prodQuote2.setAttr1("noAfterInsert");
                prodQuoteService.insert(prodQuote2);
            }

        }
    }


    @Override
    public List<QuoteRound> queryByProjectId(QuoteRound quoteRound1) {
        return quoteRoundMapper.queryByProjectId(quoteRound1);
    }

    @Override
    public void afterDelete(QuoteRound entity) throws Exception {
        super.beforDelete(entity);
        //删除报价轮次同时删除关联零件报价表的总成及零部件报价信息
        //根据当前报价轮次ID去零件报价表查询零件，并删除所有信息，不区分总成和零件（总成）
        ProdQuote prodQuote = new ProdQuote();
        prodQuote.setQuoteRoundId(entity.getId());
        prodQuoteService.deleteByQuoteId(prodQuote);
    }


    /**
     * 查询报价轮次，同时更新加和计算
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public List<QuoteRound> queryByExamplePage(QuoteRound entity) throws Exception {
        //查询当前页之前，将零件报价表的各字段加和，放到entity中
        //1.查询本项目，本轮次,基本信息表父总成字段为空的已生成的总成报价信息，提取部分字段值
        List<QuoteRound> quoteRounds = quoteRoundMapper.queryByExamplePage(entity);
        for (QuoteRound quoteRound : quoteRounds) {
            ProdQuote prodQuote = new ProdQuote();
            prodQuote.setProjectHeadId(quoteRound.getProjectHeadId());
            prodQuote.setQuoteRoundId(quoteRound.getId());
            prodQuote.setAttr1("assemblyParts");
            List<ProdQuote> prodQuotes = prodQuoteService.queryAll(prodQuote);
            BigDecimal businessPrice = new BigDecimal(0);
            BigDecimal avgCarPrice = new BigDecimal(0);
            BigDecimal assemblyMoldFee = new BigDecimal(0);
            BigDecimal asseInspectionFee = new BigDecimal(0);
            BigDecimal asseSpecEquipCost = new BigDecimal(0);
            BigDecimal factoryCost = new BigDecimal(0);
            BigDecimal zero = new BigDecimal(0);
            BigDecimal totalPartAvgCarCost = BigDecimal.ZERO;
            BigDecimal totalCusPrice = BigDecimal.ZERO;
            BigDecimal totalAcctPrice = BigDecimal.ZERO;
            BigDecimal totalAcctAvgCarPri = BigDecimal.ZERO;

            for (ProdQuote record : prodQuotes) {
                //总成商务价格（未含模摊）
                BigDecimal businessPriceNo = record.getBusinessPriceNo() == null ? businessPrice : record.getBusinessPriceNo();
                businessPrice = businessPrice.add(businessPriceNo);
                //总成均车价
                BigDecimal averageCarPrice = record.getAverageCarPrice() == null ? zero : record.getAverageCarPrice();
                avgCarPrice = avgCarPrice.add(averageCarPrice);
                //总成模具费
                BigDecimal averageCarPric = record.getToolFee() == null ? zero : record.getToolFee();
                assemblyMoldFee = assemblyMoldFee.add(averageCarPric);
                //总成检具费
                BigDecimal asseInspectionFe = record.getSubmitFee() == null ? zero : record.getSubmitFee();
                asseInspectionFee = asseInspectionFee.add(asseInspectionFe);
                //总成专用设备费
                BigDecimal asseSpecEquipCos = record.getSpecEquipFee() == null ? zero : record.getSpecEquipFee();
                asseSpecEquipCost = asseSpecEquipCost.add(asseSpecEquipCos);
                //总成本 = 工厂成本（未含模摊）之和
                BigDecimal factoryCos = record.getFactoryCost() == null ? zero : record.getFactoryCost();
                factoryCost = factoryCost.add(factoryCos);

                BigDecimal partAvgCarCost = BigDecimalUtils.changeNullToZero(record.getPartAvgCarCost());
                totalPartAvgCarCost = totalPartAvgCarCost.add(partAvgCarCost);

                BigDecimal cusPrice = BigDecimalUtils.changeNullToZero(record.getAcctPrice());
                totalCusPrice = totalCusPrice.add(cusPrice);

                BigDecimal acctPrice = BigDecimalUtils.changeNullToZero(record.getAcctPrice());
                totalAcctPrice = totalAcctPrice.add(acctPrice);
                BigDecimal acctAvgCarPri = BigDecimalUtils.changeNullToZero(record.getAcctPriceCarPrice());
                totalAcctAvgCarPri = totalAcctAvgCarPri.add(acctAvgCarPri);
            }

            if (!(totalAcctPrice.compareTo(BigDecimal.ZERO) == 0)) {
                quoteRound.setCustPrice(totalAcctPrice);
                quoteRound.setCustAvgCarPrice(totalAcctAvgCarPri);
                BigDecimal custMargin = totalAcctPrice.subtract(factoryCost).divide(totalAcctPrice,4,BigDecimal.ROUND_HALF_UP);
                quoteRound.setCustGrossMargin(custMargin);
            }
            quoteRound.setBusinessPrice(businessPrice);
            quoteRound.setAvgCarPrice(avgCarPrice);
            quoteRound.setMouldCostSum(assemblyMoldFee.add(asseInspectionFee).add(asseSpecEquipCost));
            quoteRound.setFactoryCost(factoryCost);
            quoteRound.setPartAvgCarCost(totalPartAvgCarCost);
            //处理除数为零情况
            if (zero.compareTo(businessPrice) == 0) {
                quoteRound.setGrossMargin(businessPrice);
            } else {
                //毛利率：（商务价格-总成本）/商务价格
                quoteRound.setGrossMargin((businessPrice.subtract(factoryCost)).divide(businessPrice,4,BigDecimal.ROUND_HALF_UP));
            }
        }

        return quoteRounds;
    }


    /**
     * 报价类型已存在RFQ报价时，当选择RFI报价时，无法成功保存，报错并提示“当前报价类型为RFQ报价，请正确填写后重新保存。”
     * 新创建的一轮报价的“报价日期”必须大于上一轮次的“报价日期”，否则保存时提示“请正确填写当前报价轮次的报价日期后重新保存该轮报价信息”
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforUpsert(QuoteRound entity) throws Exception {
        super.beforUpsert(entity);
        //保存当前报价轮次时校验最新轮次报价类型是否为RFQ,是的话，entity的报价类型如果为RFI则报错
        QuoteRound quoteRound = new QuoteRound();
        quoteRound.setProjectHeadId(entity.getProjectHeadId());
        List<QuoteRound> quoteRounds = quoteRoundMapper.queryByExamplePage(entity);
        if (quoteRounds.size() != 0) {
            QuoteRound quoteRound1 = quoteRounds.get(0);
            if ("RFQQuate".equals(quoteRound1.getQuoteType())) {
                if ("RFIQuate".equals(entity.getQuoteType())) {
                    //当前报价类型为RFQ报价，请正确填写后重新保存！
                    throw new ServiceException("IYUPROJECT-005");
                }
            }
            //若entity报价日期早于最新轮次报价日期则报错
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String oldquote = quoteRound1.getQuoteDate();
            String newquote = entity.getQuoteDate();
            Date oldtime = df.parse(oldquote);
            Date newtime = df.parse(newquote);
            if (oldtime.after(newtime)) {
                //请正确填写当前报价轮次的报价日期后重新保存！
                throw new ServiceException("IYUPROJECT-006");
            }
        }

    }

   /* @Override
    @Transactional
    public void compute(QuoteRound quoteRound) throws Exception {
        if (null == quoteRound.getProjectHeadId()) {
            throw new RuntimeException("项目id不能为空!");
        }
        if (null == quoteRound.getGrossMargin()) {
            throw new RuntimeException("毛利率不能为空!");
        }
        BigDecimal grossMargin = quoteRound.getGrossMargin();
        if (grossMargin.compareTo(BigDecimal.ZERO) <= 0
                || grossMargin.compareTo(BigDecimal.ONE) >= 0) {
            throw new RuntimeException("毛利率必须在0-1之间!");
        }
        // 1、根据projectHeadId 删除历史数据
        // quoteRoundMapper.deleteByProjectId(quoteRound);
        // 2、根据项目id 查询
        List<QuotePartsCostLine> quotePartsCostLineList = quotePartsCostLineMapper.queryByProjectId(quoteRound.getProjectHeadId());
        QuotePartsCostLine firstPartsCostLine = null;
        Map<String, List<QuotePartsCostLine>> tempMap = new HashMap<>();
        List<QuoteRound> toInsertList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(quotePartsCostLineList)) {
            firstPartsCostLine = quotePartsCostLineList.get(0);
            for (QuotePartsCostLine tempQuotePartsCostLine : quotePartsCostLineList) {
                // 按照产品零件号、产品名称分组
                String cpljh = tempQuotePartsCostLine.getCpljh();
                String cpmc = tempQuotePartsCostLine.getCpmc();
                String key = cpljh + cpmc;
                if (tempMap.containsKey(key)) {
                    List<QuotePartsCostLine> list = tempMap.get(key);
                    list.add(tempQuotePartsCostLine);
                } else {
                    List<QuotePartsCostLine> list = new ArrayList<>();
                    list.add(tempQuotePartsCostLine);
                    tempMap.put(key, list);
                }
            }
            for (String key : tempMap.keySet()) {
                List<QuotePartsCostLine> tList = tempMap.get(key);
                // 构建一级数据
                QuoteRound firstData = this.createFirstData(quoteRound.getProjectHeadId(), tList);
                toInsertList.add(firstData);
                // 构建二级数据
                QuoteRound secondData = this.createSecondData(quoteRound.getProjectHeadId(), tList, quoteRound.getGrossMargin());
                toInsertList.add(secondData);
            }
        }
        if (null != toInsertList && toInsertList.size() > 0) {
            for (QuoteRound toInsertData : toInsertList) {
                quoteRoundMapper.insert(toInsertData);
            }
        }
    }*/

    @Override
    @Transactional
    public void compute(QuoteRound quoteRound) throws Exception {
        if (null == quoteRound.getProjectHeadId()) {
            throw new RuntimeException("项目id不能为空!");
        }
        BigDecimal grossMargin = quoteRound.getGrossMargin();
        if (null == grossMargin) {
            throw new RuntimeException("毛利率不能为空!");
        }
        BigDecimal weight = quoteRound.getWeight();
        if (null == weight) {
            throw new RuntimeException("权重不能为空!");
        }
        if (grossMargin.compareTo(BigDecimal.ZERO) <= 0
                || grossMargin.compareTo(BigDecimal.ONE) >= 0) {
            throw new RuntimeException("毛利率必须在0-1之间!");
        }
        // 1、根据projectHeadId 查询报价头
        QuotePartsCost quotePartsCostParam = new QuotePartsCost();
        quotePartsCostParam.setProjectId(quoteRound.getProjectHeadId());
        List<QuotePartsCost> toHandleQuotePartsCostList = quotePartsCostMapper.queryByProjectId(quotePartsCostParam);
        // 2、根据项目id 查询 报价行
        List<QuotePartsCostLine> toHandleQuotePartsCostLineList = quotePartsCostLineMapper.queryByProjectId(quoteRound.getProjectHeadId());
        if (CollectionUtils.isEmpty(toHandleQuotePartsCostList) || CollectionUtils.isEmpty(toHandleQuotePartsCostLineList)) {
            throw new RuntimeException("计算数据异常，请查询后确认!");
        }
        // 3、组装map数据
        Map<Long, QuotePartsCost> quotePartsCostMap = new HashMap<>();
        Map<Long, List<QuotePartsCostLine>> quotePartsCostLineMap = new HashMap<>();
        for (QuotePartsCost quotePartsCost : toHandleQuotePartsCostList) {
            quotePartsCostMap.put(quotePartsCost.getId(), quotePartsCost);
        }
        for (QuotePartsCostLine quotePartsCostLine : toHandleQuotePartsCostLineList) {
            Long headId = quotePartsCostLine.getHeadId();
            if (quotePartsCostLineMap.keySet().contains(headId)) {
                quotePartsCostLineMap.get(headId).add(quotePartsCostLine);
            } else {
                List<QuotePartsCostLine> quotePartsCostLineList = new ArrayList<>();
                quotePartsCostLineList.add(quotePartsCostLine);
                quotePartsCostLineMap.put(headId, quotePartsCostLineList);
            }
        }
        logger.info(quotePartsCostMap);
        logger.info(quotePartsCostLineMap);

        for (Long quotePartsCostId : quotePartsCostMap.keySet()) {
            QuotePartsCost tempQuotePartsCost = quotePartsCostMap.get(quotePartsCostId);
            List<QuotePartsCostLine> tempQuotePartsCostLineList = quotePartsCostLineMap.get(quotePartsCostId);

            // 材料成本金额（不含税）(元)
            BigDecimal clcbjeSum = BigDecimal.ZERO;
            // 外购件成本
            BigDecimal wgjcbSum = BigDecimal.ZERO;
            // 外协成本金额
            BigDecimal wxcbjeSum = BigDecimal.ZERO;
            // 人工成本（元/件）
            BigDecimal rgcbSum = BigDecimal.ZERO;
            // 设备成本_自制（元/件）
            BigDecimal sbcbzzSum = BigDecimal.ZERO;
            // 制造成本_自制(元/件)
            BigDecimal zzcbzzSum = BigDecimal.ZERO;
            // 模具费、检具费、工装费、试验费
            BigDecimal mjfSum = BigDecimal.ZERO;
            BigDecimal jjfSum = BigDecimal.ZERO;
            BigDecimal gzfSum = BigDecimal.ZERO;
            BigDecimal syfSum = BigDecimal.ZERO;
            // 模摊成本
            BigDecimal mtcbSum = BigDecimal.ZERO;
            // 周转器具成本（元/件）
            BigDecimal zzqjcbSum = BigDecimal.ZERO;



            // A、零件
            for (QuotePartsCostLine tempQuotePartsCostLine : tempQuotePartsCostLineList) {

                logger.info("tempQuotePartsCostLine is " + JsonUtil.toString(tempQuotePartsCostLine));

                // 材料单价(不含税)（元/kg） = 材料单价/(1-毛利率)
                try {
                    BigDecimal cldj = tempQuotePartsCostLine.getCldj().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setCldj(cldj);
                    // 材料成本金额（不含税）(元)
                    clcbjeSum = clcbjeSum.add(tempQuotePartsCostLine.getClcbje());
                } catch (Exception e) {
                    throw new RuntimeException("A材料单价(不含税)（元/kg）计算异常：" + e.getMessage());
                }

                // 外购件价格
                try {
                    BigDecimal wgjjg = tempQuotePartsCostLine.getWgjjg().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setWgjjg(wgjjg);
                } catch (Exception e) {
                    throw new RuntimeException("A外购件价格计算异常：" + e.getMessage());
                }

                // 外购件成本
                try {
                    wgjcbSum = wgjcbSum.add(tempQuotePartsCostLine.getWgjcb());
                } catch (Exception e) {
                    throw new RuntimeException("A外购件成本计算异常：" + e.getMessage());
                }

                // 外协单价/(1-毛利率)
                try {
                    BigDecimal wxdj = tempQuotePartsCostLine.getWxdj().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setWxdj(wxdj);
                } catch (Exception e) {
                    throw new RuntimeException("A外协单价计算异常：" + e.getMessage());
                }

                // 外协成本金额
                try {
                    wxcbjeSum = wxcbjeSum.add(tempQuotePartsCostLine.getWxcbje());
                } catch (Exception e) {
                    throw new RuntimeException("A外协成本金额计算异常：" + e.getMessage());
                }

                // 工序节拍（s） = 成本明细工序节拍+5秒
                try {
                    BigDecimal gxjp = tempQuotePartsCostLine.getGxjp().add(new BigDecimal(5));
                    tempQuotePartsCostLine.setGxjp(gxjp);
                } catch (Exception e) {
                    throw new RuntimeException("A工序节拍（s）计算异常：" + e.getMessage());
                }

                // 每小时人工成本（元/小时） = 30
                tempQuotePartsCostLine.setMxsrgcb(new BigDecimal(30));

                // 人工成本（元/件）
                try {
                    rgcbSum = rgcbSum.add(tempQuotePartsCostLine.getRgcb());
                } catch (Exception e) {
                    throw new RuntimeException("A人工成本（元/件）计算异常：" + e.getMessage());
                }

                // 每小时设备成本_自制（元/小时） = 每小时设备成本_自制/0.85
                try {
                    BigDecimal mxssbcbzz = tempQuotePartsCostLine.getMxssbcbzz().divide(
                            new BigDecimal(0.85), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setMxssbcbzz(mxssbcbzz);
                } catch (Exception e) {
                    throw new RuntimeException("A每小时设备成本_自制（元/小时）计算异常：" + e.getMessage());
                }

                // 设备成本_自制（元/件）
                try {
                    sbcbzzSum = sbcbzzSum.add(tempQuotePartsCostLine.getSbcbzz());
                } catch (Exception e) {
                    throw new RuntimeException("A设备成本_自制（元/件）计算异常：" + e.getMessage());
                }
                // 制造成本_自制(元/件)
                try {
                    zzcbzzSum = zzcbzzSum.add(tempQuotePartsCostLine.getZzcbzz());
                } catch (Exception e) {
                    throw new RuntimeException("A制造成本_自制(元/件)计算异常：" + e.getMessage());
                }

                // 模具费
                try {
                    BigDecimal mjf = tempQuotePartsCostLine.getMjf().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setMjf(mjf);
                    mjfSum = mjfSum.add(mjf);
                } catch (Exception e) {
                    throw new RuntimeException("A模具费计算异常：" + e.getMessage());
                }

                // 检具费
                try {
                    BigDecimal jjf = tempQuotePartsCostLine.getJjf().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setJjf(jjf);
                    jjfSum = jjfSum.add(jjf);
                } catch (Exception e) {
                    throw new RuntimeException("A检具费计算异常：" + e.getMessage());
                }
                // 工装费
                try {
                    BigDecimal gzf = tempQuotePartsCostLine.getGzf().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setGzf(gzf);
                    gzfSum = gzfSum.add(gzf);
                } catch (Exception e) {
                    throw new RuntimeException("A工装费计算异常：" + e.getMessage());
                }
                // 试验费
                try {
                    BigDecimal syf = tempQuotePartsCostLine.getSyf().divide(
                            BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP);
                    tempQuotePartsCostLine.setSyf(syf);
                    syfSum = syfSum.add(syf);
                } catch (Exception e) {
                    throw new RuntimeException("A试验费计算异常：" + e.getMessage());
                }

                // 模摊成本
                try {
                    mtcbSum = mtcbSum.add(tempQuotePartsCostLine.getMtcb());
                } catch (Exception e) {
                    throw new RuntimeException("A模摊成本计算异常：" + e.getMessage());
                }

                // 周转器具成本（元/件）
                try {
                    zzqjcbSum = zzqjcbSum.add(tempQuotePartsCostLine.getZzqjcb());
                } catch (Exception e) {
                    throw new RuntimeException("A周转器具成本（元/件）计算异常：" + e.getMessage());
                }

            }
            // B、总成件
            // 材料成本金额（不含税）(元) = 各子明细项之和
            tempQuotePartsCost.setClcbje(clcbjeSum);
            // 外购件成本
            tempQuotePartsCost.setWgjcb(wgjcbSum);
            // 外协成本金额
            tempQuotePartsCost.setWxcbje(wxcbjeSum);
            // // 人工成本（元/件）
            tempQuotePartsCost.setRgcb(rgcbSum);
            // 设备成本_自制（元/件）
            tempQuotePartsCost.setSbcbzz(sbcbzzSum);
            // 制造成本_自制(元/件)
            tempQuotePartsCost.setZzcbzz(zzcbzzSum);
            // 模具费、检具费、工装费、试验费
            tempQuotePartsCost.setMjf(mjfSum);
            tempQuotePartsCost.setJjf(jjfSum);
            tempQuotePartsCost.setGzf(gzfSum);
            tempQuotePartsCost.setSyf(syfSum);
            // 模摊成本
            tempQuotePartsCost.setMtcb(mtcbSum);
            // 运费、仓储费 = 自身/(1 - 毛利率 + 5%)
            try {
                BigDecimal yf = tempQuotePartsCost.getYf().divide(
                        (BigDecimal.ONE.subtract(grossMargin)).add(new BigDecimal(0.05)), 2, BigDecimal.ROUND_HALF_UP);
                tempQuotePartsCost.setYf(yf);
            } catch (Exception e) {
                throw new RuntimeException("B运费计算异常：" + e.getMessage());
            }
            try {
                BigDecimal ccf = tempQuotePartsCost.getCcf().divide(
                        (BigDecimal.ONE.subtract(grossMargin).add(new BigDecimal(0.05))), 2, BigDecimal.ROUND_HALF_UP);
                tempQuotePartsCost.setCcf(ccf);
            } catch (Exception e) {
                throw new RuntimeException("B仓储费计算异常：" + e.getMessage());
            }
            // 周转器具成本（元/件）
            tempQuotePartsCost.setZzqjcb(zzqjcbSum);
            // 工厂成本_自制 （不含模摊） = 材料成本金额+外购件成本+制造成本+运维+仓储费+周转器具成本（元/件）
            BigDecimal gccbzzbhmt = BigDecimal.ZERO;
            try {
                gccbzzbhmt = tempQuotePartsCost.getClcbje()
                        .add(tempQuotePartsCost.getWgjcb())
                        .add(tempQuotePartsCost.getZzcbzz())
                        .add(tempQuotePartsCost.getYf())
                        .add(tempQuotePartsCost.getCcf())
                        .add(tempQuotePartsCost.getZzqjcb());
                tempQuotePartsCost.setGccbzzbhmt(gccbzzbhmt);
            } catch (Exception e) {
                throw new RuntimeException("B工厂成本_自制（不含模摊）计算异常：" + e.getMessage());
            }

            // 管理费、利润
            try {
                tempQuotePartsCost.setGlf(gccbzzbhmt.multiply(weight));
            } catch (Exception e) {
                throw new RuntimeException("B管理费计算异常：" + e.getMessage());
            }
            try {
                tempQuotePartsCost.setLr(gccbzzbhmt.multiply(weight));
            } catch (Exception e) {
                throw new RuntimeException("B利润计算异常：" + e.getMessage());
            }

            // 商务报价 （不含模摊） = 工厂成本_自制（不含模摊）+管理费用+利润
            BigDecimal swbjbhmt = BigDecimal.ZERO;
            try {
                swbjbhmt = gccbzzbhmt.add(tempQuotePartsCost.getGlf()).add(tempQuotePartsCost.getLr());
                tempQuotePartsCost.setSwbjbhmt(swbjbhmt);
            } catch (Exception e) {
                throw new RuntimeException("B商务报价 （不含模摊）计算异常：" + e.getMessage());
            }

            // 毛利_自制 （不含模摊） = 商务报价（不含模摊）-成本明细表里面“工厂成本_自制 （不含模摊）”
            try {
                tempQuotePartsCost.setMlzz(swbjbhmt.subtract(gccbzzbhmt));
            } catch (Exception e) {
                throw new RuntimeException("B毛利_自制（不含模摊）计算异常：" + e.getMessage());
            }

            // 毛利率   （不含模摊） = 毛利_自制  （不含模摊）/商务报价 （不含模摊）
            BigDecimal mll = BigDecimal.ZERO;
            try {
                mll = tempQuotePartsCost.getMlzz().divide(swbjbhmt, 2, BigDecimal.ROUND_HALF_UP);
                tempQuotePartsCost.setMll(mll.toString());
            } catch (Exception e) {
                throw new RuntimeException("B毛利率（不含模摊）计算异常：" + e.getMessage());
            }

            // 工厂成本_自制（含模摊） = 毛利_自制  （不含模摊）/商务报价 （不含模摊）
            BigDecimal gccbzzhmt = BigDecimal.ZERO;
            try {
                gccbzzhmt = mll.divide(swbjbhmt, 2, BigDecimal.ROUND_HALF_UP);
                tempQuotePartsCost.setGccbzzhmt(gccbzzhmt);
            } catch (Exception e) {
                throw new RuntimeException("B工厂成本_自制（含模摊）计算异常：" + e.getMessage());
            }
            // 商务报价（含模摊） = 工厂成本_自制（含模摊）+管理费用+利润
            BigDecimal swbjhmt = BigDecimal.ZERO;
            try {
                swbjhmt = gccbzzhmt.add(tempQuotePartsCost.getGlf()).add(tempQuotePartsCost.getLr());
                tempQuotePartsCost.setSwbjhmt(swbjhmt);
            } catch (Exception e) {
                throw new RuntimeException("B商务报价（含模摊）计算异常：" + e.getMessage());
            }

            // 毛利_自制（含模摊） = 商务报价（含模摊）-成本明细表里面“工厂成本_自制     （含模摊）”
            BigDecimal mlzzhmt = BigDecimal.ZERO;
            try {
                mlzzhmt = swbjhmt.subtract(gccbzzhmt);
                tempQuotePartsCost.setMlzzhmt(mlzzhmt);
            } catch (Exception e) {
                throw new RuntimeException("B商务报价（含模摊）计算异常：" + e.getMessage());
            }
            // 毛利率（含模摊） = 毛利_自制  （含模摊）/商务报价 （含模摊）
            try {
                BigDecimal mllhmt = mlzzhmt.divide(swbjbhmt, 2, BigDecimal.ROUND_HALF_UP);
                tempQuotePartsCost.setMllhmt(mllhmt.toString());
            } catch (Exception e) {
                throw new RuntimeException("B毛利率（含模摊）计算异常：" + e.getMessage());
            }

        }


        logger.info("---------------------------------------------------------------------------------");

        // 更新
        for (Long quotePartsCostId : quotePartsCostMap.keySet()) {
            QuotePartsCost tempQuotePartsCost = quotePartsCostMap.get(quotePartsCostId);
            // 先删除，在插入
            ComputeQuotePartsCost delComputeQuotePartsCost = new ComputeQuotePartsCost();
            delComputeQuotePartsCost.setId(tempQuotePartsCost.getId());
            computeQuotePartsCostMapper.deleteById(delComputeQuotePartsCost);
            computeQuotePartsCostMapper.quotePartsCostInsert(tempQuotePartsCost);

            List<QuotePartsCostLine> tempQuotePartsCostLineList = quotePartsCostLineMap.get(quotePartsCostId);
            for (QuotePartsCostLine tempQuotePartsCostLine : tempQuotePartsCostLineList) {
                ComputeQuotePartsCostLine computeQuotePartsCostLine = new ComputeQuotePartsCostLine();
                // 先删除，在插入
                computeQuotePartsCostLine.setId(tempQuotePartsCostLine.getId());
                computeQuotePartsCostLineMapper.deleteById(computeQuotePartsCostLine);
                /*Date date = new Date();
                tempQuotePartsCostLine.setCreated(DateUtil.dateToStr(date, null));
                tempQuotePartsCostLine.setLastUpdated(DateUtil.dateToStr(date, null));*/
                computeQuotePartsCostLineMapper.quotePartsCostLineInsert(tempQuotePartsCostLine);
            }
        }

        // 计算完成 将项目表中 是否对外报价计算 is_quote_compute 更新为Y
        /*Project project = new Project();
        project.setId(quoteRound.getProjectHeadId());
        project.setIsQuoteCompute("Y");
        projectMapper.updateIsQuoteCompute(project);*/

    }





    private QuoteRound createFirstData(Long projectId, List<QuotePartsCostLine> tList) {
        QuoteRound quoteRound = new QuoteRound();
        quoteRound.setId(keyGenerateDao.keyGenerate());
        quoteRound.setProjectHeadId(projectId);
        // 产品零件号
        quoteRound.setCpljh(tList.get(0).getCpljh());
        // 产品名称
        quoteRound.setCpmc(tList.get(0).getCpmc());
        // 材料单价(不含税)（元/kg） cldj
        if (null == tList.get(0).getCldj()) {
            quoteRound.setCldj(BigDecimal.ZERO);
        } else {
            quoteRound.setCldj(tList.get(0).getCldj());
        }
        // 外购件价格
        BigDecimal wgjjg = BigDecimal.ZERO;
        // 外协单价
        BigDecimal wxdj = BigDecimal.ZERO;
        // 模具费
        BigDecimal mjf = BigDecimal.ZERO;
        // 检具费
        BigDecimal jjf = BigDecimal.ZERO;
        // 工装费
        BigDecimal gzf = BigDecimal.ZERO;
        // 试验费
        BigDecimal syf = BigDecimal.ZERO;
        // 运费
        if (null == tList.get(0).getYf()) {
            quoteRound.setYf(BigDecimal.ZERO);
        } else {
            quoteRound.setYf(tList.get(0).getYf());
        }
        // 仓储费
        if (null == tList.get(0).getCcf()) {
            quoteRound.setCldj(BigDecimal.ZERO);
        } else {
            quoteRound.setCcf(tList.get(0).getCcf());
        }
        for (QuotePartsCostLine quotePartsCostLine : tList) {
            wgjjg = wgjjg.add(null == quotePartsCostLine.getWgjjg() ? BigDecimal.ZERO : quotePartsCostLine.getWgjjg());
            wxdj = wxdj.add(null == quotePartsCostLine.getWxdj() ? BigDecimal.ZERO : quotePartsCostLine.getWxdj());
            mjf = mjf.add(null == quotePartsCostLine.getMjf() ? BigDecimal.ZERO : quotePartsCostLine.getMjf());
            jjf = jjf.add(null == quotePartsCostLine.getJjf() ? BigDecimal.ZERO : quotePartsCostLine.getJjf());
            gzf = gzf.add(null == quotePartsCostLine.getGzf() ? BigDecimal.ZERO : quotePartsCostLine.getGzf());
            syf = syf.add(null == quotePartsCostLine.getSyf() ? BigDecimal.ZERO : quotePartsCostLine.getSyf());
        }
        quoteRound.setWgjjg(wgjjg);
        quoteRound.setWxdj(wxdj);
        quoteRound.setMjf(mjf);
        quoteRound.setJjf(jjf);
        quoteRound.setGzf(gzf);
        quoteRound.setSyf(syf);
        return quoteRound;
    }

    private QuoteRound createSecondData(Long projectId, List<QuotePartsCostLine> tList, BigDecimal grossMargin) {
        QuoteRound quoteRound = new QuoteRound();
        quoteRound.setId(keyGenerateDao.keyGenerate());
        quoteRound.setProjectHeadId(projectId);
        // 产品零件号
        quoteRound.setCpljh(tList.get(0).getCpljh());
        // 产品名称
        quoteRound.setCpmc(tList.get(0).getCpmc());
        // 材料单价(不含税)（元/kg） cldj
        BigDecimal cldj = BigDecimal.ZERO;
        // 外购件价格
        BigDecimal wgjjg = BigDecimal.ZERO;
        // 工序节拍（s）
        BigDecimal gxjp = BigDecimal.ZERO;
        // 每小时设备成本_自制（元/小时）
        BigDecimal mxssbcbzz = BigDecimal.ZERO;
        // 外协单价
        BigDecimal wxdj = BigDecimal.ZERO;
        // 模具费
        BigDecimal mjf = BigDecimal.ZERO;
        // 检具费
        BigDecimal jjf = BigDecimal.ZERO;
        // 工装费
        BigDecimal gzf = BigDecimal.ZERO;
        // 试验费
        BigDecimal syf = BigDecimal.ZERO;

        BigDecimal yfdj = BigDecimal.ZERO;
        BigDecimal yf = BigDecimal.ZERO;
        BigDecimal ccf = BigDecimal.ZERO;
        BigDecimal zzqjdj = BigDecimal.ZERO;
        for (QuotePartsCostLine quotePartsCostLine : tList) {
            cldj = cldj.add(null == quotePartsCostLine.getCldj() ? BigDecimal.ZERO : quotePartsCostLine.getCldj());
            wgjjg = wgjjg.add(null == quotePartsCostLine.getWgjjg() ? BigDecimal.ZERO : quotePartsCostLine.getWgjjg());
            wxdj = wxdj.add(null == quotePartsCostLine.getWxdj() ? BigDecimal.ZERO : quotePartsCostLine.getWxdj());
            gxjp = gxjp.add(null == quotePartsCostLine.getGxjp() ? BigDecimal.ZERO : quotePartsCostLine.getGxjp());
            mxssbcbzz = mxssbcbzz.add(null ==  quotePartsCostLine.getMxssbcbzz()
                    ? BigDecimal.ZERO : quotePartsCostLine.getMxssbcbzz());

            mjf = mjf.add(null == quotePartsCostLine.getMjf() ? BigDecimal.ZERO : quotePartsCostLine.getMjf());
            jjf = jjf.add(null == quotePartsCostLine.getJjf() ? BigDecimal.ZERO : quotePartsCostLine.getJjf());
            gzf = gzf.add(null == quotePartsCostLine.getGzf() ? BigDecimal.ZERO : quotePartsCostLine.getGzf());
            syf = syf.add(null == quotePartsCostLine.getSyf() ? BigDecimal.ZERO : quotePartsCostLine.getSyf());

            yfdj = yfdj.add(null == quotePartsCostLine.getYfdj() ? BigDecimal.ZERO : quotePartsCostLine.getYfdj());
            yf = yf.add(null == quotePartsCostLine.getYf() ? BigDecimal.ZERO : quotePartsCostLine.getYf());
            ccf = ccf.add(null == quotePartsCostLine.getCcf() ? BigDecimal.ZERO : quotePartsCostLine.getCcf());
            zzqjdj = zzqjdj.add(null == quotePartsCostLine.getZzqjdj()
                    ? BigDecimal.ZERO : quotePartsCostLine.getZzqjdj());
        }
        // 材料单价(不含税)（元/kg） cldj = 行上零件明细材料单价(不含税)（元/kg）之和除以(1-毛利率)，得出最终值
        quoteRound.setCldj(cldj.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 外购件价格
        quoteRound.setWgjjg(wgjjg.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 外协单价
        quoteRound.setWxdj(wxdj.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 工序节拍（s） 行上零件工序节拍（s）都增加+5秒，得出最终值
        quoteRound.setGxjp(gxjp.add(new BigDecimal(5)));
        // 每小时设备成本_自制（元/小时）
        quoteRound.setMxssbcbzz(mxssbcbzz.divide(new BigDecimal(0.8), 2, BigDecimal.ROUND_HALF_UP));
        // 模具费
        quoteRound.setMjf(mjf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 检具费
        quoteRound.setJjf(jjf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 工装费
        quoteRound.setGzf(gzf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 试验费
        quoteRound.setSyf(syf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 运费单价(每立方/吨)
        quoteRound.setYfdj(yfdj.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 运费
        quoteRound.setYf(yf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 仓储费
        quoteRound.setCcf(ccf.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));
        // 周转器具单价
        quoteRound.setZzqjdj(zzqjdj.divide(BigDecimal.ONE.subtract(grossMargin), 2, BigDecimal.ROUND_HALF_UP));

        return quoteRound;
    }
}
